package com.qst.config;

import com.qst.interceptor.ResourcesInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
@PropertySource("classpath:ignoreUrl.properties") // 读取用户权限配置文件
@ComponentScan({"com.qst.controller"})
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    // 通过分隔符,将配置文件中的路径放入到list变量中
    @Value("#{'${ignoreUrl}'.split(',')}")
    private List<String> ignoreUrl;

    // 将允许的路径放置在自定义拦截器对象中
    @Bean
    public ResourcesInterceptor resourcesInterceptor(){
        return new ResourcesInterceptor(ignoreUrl);
    }

    //开启对静态资源的访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    // 配置视图解析器
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/admin/",".jsp");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


registry.addInterceptor(resourcesInterceptor()).addPathPatterns("/**").excludePathPatterns("/css/**","/js/**","/img/**");
    }
}
